package com.lwg.vhr.controller.system;

import com.lwg.vhr.model.Hr;
import com.lwg.vhr.model.Role;
import com.lwg.vhr.service.impl.HrServiceImpl;
import com.lwg.vhr.service.impl.RoleServiceImpl;
import com.lwg.vhr.model.RespBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author: lwg
 * @Date: 2021/8/18 21:58
 * @Description: 人事信息管理
 */

@RestController
@RequestMapping("/system/hr")
public class HrController {

    @Autowired
    private HrServiceImpl hrService;

    @Autowired
    private RoleServiceImpl roleService;

    @GetMapping("/")
    public List<Hr> getAllHrs(String keywords){
        return hrService.getAllHrs(keywords);
    }

    /*更新用户的enabled状态*/
    @PutMapping("/")
    public RespBean updateHr(@RequestBody Hr hr){
        if (hrService.updateHr(hr)==1){
            return RespBean.ok("更新成功");
        }
        return RespBean.error("更新失败");
    }
    /*获得所有的角色*/
    @GetMapping("/roles")
    public List<Role> getAllRoles(){
        return roleService.getAllRoles();
    }

    /*更新用户角色栏，先删除后添加*/
    @PutMapping("/role")
    public RespBean updateHrRole(Integer hrid,Integer[] rids){
        if (hrService.updateHrRole(hrid,rids)){
            return RespBean.ok("更新成功");
        }
        return RespBean.error("更新失败");
    }

    @DeleteMapping("/{id}")
    public RespBean deleteHrById(@PathVariable Integer id) {
        if (hrService.deleteHrById(id) == 1) {
            return RespBean.ok("删除成功!");
        }
        return RespBean.error("删除失败!");
    }
}
